//
//  gongyingViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/22.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "LGLSearchBar.h"
#import "PTLMaskingView.h"
#import "qiyexqViewController.h"
#import "gongyingViewController.h"
#import "gongyingTableViewCell.h"
@interface gongyingViewController ()
@property(nonatomic,strong)LGLSearchBar * searchBar;

@end

@implementation gongyingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  _searchBar = [[LGLSearchBar alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 40) searchBarStyle:LGLSearchBarStyleDefault];
  [_searchBar setSearchBarBordeWidth:0.1 bordColor:[UIColor blackColor] bordRadius:0];

    [_searchBar searchBarTextSearchTextBlock:^(NSString *searchText) {
        NSLog(@"%@", searchText);
        _cpname = searchText;
        [self loadData];
    }];
//    searchBar.barBackgroudColor = [UIColor yellowColor];
//        searchBar.textBackgroudColor = [UIColor redColor];

//    [UIColor colorWithHexString:@"f0eff5"];
//[searchBar setSearchBarBordeWidth:0 bordColor:nil bordRadius:0];
    [self.view addSubview:_searchBar];
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, KScreenWidth,KScreenHeight-kTopHeight-40-kTabBarHeight-40) style:UITableViewStylePlain];
   self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;


_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
[self.view addSubview:_tableView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

     __weak typeof(self) weakSelf = self;
[self.tableView addHeaderWithCallback:^{

        weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
   if (weakSelf.lastPage==0 ){

     [weakSelf loadMore];

        }else{
            
            [weakSelf.tableView footerEndRefreshing];
            [weakSelf.view makeToast:NSLocalizedString(@"没有更多了", nil)];
            
        }
        
    }];
    // Do any additional setup after loading the view.
}
-(void)viewDidLayoutSubviews

{

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [self.tableView setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        [self.tableView setLayoutMargins:UIEdgeInsetsZero];

    }

}



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

 

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

 

}
- (void)loadData{


    [MBProgressHUD showHUDAddedTo:self.view animated:YES];


 NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
       dics[@"pageNumber"] = @"1";
       dics[@"status"] = @"0";
         dics[@"pageSize"] = @"20";
              [dics setValue:_cpname forKey:@"cpname"];
     [dics setValue:_priceId forKey:@"priceId"];
     [dics setValue:_proTypeId forKey:@"proTypeId"];
     [dics setValue:_categoryId forKey:@"categoryId"];
     [dics setValue:_areaid forKey:@"areaid"];
   NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/company/list?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  _searchBar.text = _cpname;

    NSLog(@"%@",dictionary);
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
      _dic = dictionary;
      [self initview];
 if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
        NSDictionary *dic = dictionary[@"cppage"];
          _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
      NSArray *array = dic[@"list"];
 if (array.count==0)
        {
             [self.view makeToast:NSLocalizedString(@"没有数据", nil)];
        }
    _dataArray = [NSMutableArray array];
   for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
}
[_tableView reloadData];
            [self.tableView headerEndRefreshing];
        self.currentPage++;
   }andFailure:^{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.tableView headerEndRefreshing];


 }];
}
- (void)initview{
if (_btn) {
[_btn removeFromSuperview];
}
NSArray *array1 = _dic[@"typelist"];
    NSArray *array2 = _dic[@"pelist"];
    NSArray *array3 = _dic[@"ctglist"];
    NSArray *array4 = _dic[@"arealist"];
        NSMutableArray *listarray1 = [NSMutableArray array];
             NSDictionary *selType =_dic[@"selType"];
 if ([selType count ]!=0) {
        [listarray1 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),selType[@"dictname"]]];

}
 for (NSDictionary *dic in array1) {
    [listarray1 addObject:dic[@"dictname"]];
}

  NSMutableArray *listarray2 = [NSMutableArray array];
             NSDictionary *selPrice =_dic[@"selPrice"];
 if ([selPrice count ]!=0) {
        [listarray2 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),selPrice[@"dictname"]]];

}
 for (NSDictionary *dic in array2) {
    [listarray2 addObject:dic[@"dictname"]];
}

 NSMutableArray *listarray3 = [NSMutableArray array];
             NSDictionary *selCtg =_dic[@"selCtg"];
 if ([selCtg count ]!=0) {
        [listarray3 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),selCtg[@"ctgName"]]];

}
 for (NSDictionary *dic in array3) {
    [listarray3 addObject:dic[@"ctgName"]];
}

 NSMutableArray *listarray4 = [NSMutableArray array];
             NSDictionary *selArea =_dic[@"selArea"];
 if ([selArea count ]!=0) {
        [listarray4 addObject:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"当前选择", nil),selArea[@"areaname"]]];

}
 for (NSDictionary *dic in array4) {
    [listarray4 addObject:dic[@"areaname"]];
}

 _btn = [[PTLMenuButton alloc]initWithFrame:CGRectMake(0, 40, (KScreenWidth), 40) menuTitles:@[NSLocalizedString(@"企业性质", nil),NSLocalizedString(@"年营业额", nil),NSLocalizedString(@"采购信息list所属行业", nil),NSLocalizedString(@"所有地区", nil)]];
   NSArray * listArr1 = listarray1;
    NSArray * listArr2 = listarray2;
     NSArray * listArr3 = listarray3;
    NSArray * listArr4 = listarray4;
    _btn.type =@"shangjia";
  _btn.listTitles = @[listArr1, listArr2,listArr3,listArr4];
    _btn.delegate = self;
    [self.view addSubview:_btn];
     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 39+40, self.view.frame.size.width, 1)];
    view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
        [self.view addSubview:view];
    
    
        __weak typeof(self) weakSelf = self;

    [self.btn.listView.maskingView setButtonblock :^() {
  NSLog(@"11111");
  _cpname = @"";
    _priceId = @"";
    _proTypeId = @"";
    _categoryId = @"";
    _areaid = @"";
        [weakSelf loadData];

  }];
}
-(void)ptl_menuButton:(PTLMenuButton *)menuButton didSelectMenuButtonAtIndex:(NSInteger)index selectMenuButtonTitle:(NSString *)title listRow:(NSInteger)row rowTitle:(NSString *)rowTitle{
NSArray *array1 = _dic[@"typelist"];
    NSArray *array2 = _dic[@"pelist"];
    NSArray *array3 = _dic[@"ctglist"];
    NSArray *array4 = _dic[@"arealist"];

 if ([title isEqualToString:NSLocalizedString(@"企业性质", nil)]){
      for (NSDictionary *dic in array1) {
   if ([dic[@"dictname"]isEqualToString:rowTitle]) {
    _proTypeId = dic[@"id"];
}

}
               [self loadData];

    }else if ([title isEqualToString:NSLocalizedString(@"年营业额", nil)])
    {
      for (NSDictionary *dic in array2) {
   if ([dic[@"dictname"]isEqualToString:rowTitle]) {
    _priceId = dic[@"id"];
}

}
               [self loadData];

    }else if ([title isEqualToString:NSLocalizedString(@"采购信息list所属行业", nil)])
    {
      for (NSDictionary *dic in array3) {
   if ([dic[@"ctgName"]isEqualToString:rowTitle]) {
    _categoryId = dic[@"id"];
}

}
               [self loadData];

    }else{
      for (NSDictionary *dic in array4) {
   if ([dic[@"areaname"]isEqualToString:rowTitle]) {
    _areaid = dic[@"id"];
}

}
               [self loadData];

    }

}
- (void)loadMore{
 NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
  dics[@"pageNumber"] = [NSString stringWithFormat:@"%d",_currentPage];
       dics[@"status"] = @"0";
         dics[@"pageSize"] = @"20";
     [dics setValue:_priceId forKey:@"priceId"];
     [dics setValue:_proTypeId forKey:@"proTypeId"];
     [dics setValue:_categoryId forKey:@"categoryId"];
     [dics setValue:_areaid forKey:@"areaid"];
   NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/company/list?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
   NSLog(@"%@",dictionary);
        NSDictionary *dic = dictionary[@"cppage"];
NSArray *array = dic[@"list"];
  _lastPage = [dic[@"lastPage"]intValue];
  self.totalCounts =[dic[@"totalRow"] intValue];
   for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
}
[_tableView reloadData];
        [self.tableView footerEndRefreshing];

        self.currentPage++;

}andFailure:^{
  [self.tableView footerEndRefreshing];
      [MBProgressHUD hideHUDForView:self.view animated:YES];

 }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 
    return  120+20+10+30+6;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *CellIdentifier = @"gongyingTableViewCell";

  gongyingTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell==nil) {
        cell=[[gongyingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
 NSDictionary *dic = self.dataArray[indexPath.row];
    cell.dic = dic;
    return cell;



 }
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSDictionary *dic = _dataArray[indexPath.row];
 qiyexqViewController *vc = [[qiyexqViewController alloc]init];
 vc.pdid = dic[@"id"];
 vc.hidesBottomBarWhenPushed = YES;
 [self.navigationController pushViewController:vc animated:YES];

 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
